package com.zaofan.controller;

import com.zaofan.bean.Admin;
import com.zaofan.bean.Comment;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.CommentService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/comment")
public class CommentController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CommentService commentService = new CommentService();
	

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			String selectCityId = "";
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				if(!name.equals("cityId")){
					params.put(name, value);
				}
				else {

					selectCityId = request.getParameter(name);

				}
			}
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			if(admin.getCityId()!=null&&admin.getCityId()!=0){
				selectCityId = admin.getCityId().toString();
			}
			List<Comment> logs = commentService.findAll(admin.getCityId(), params, selectCityId);
			Long count = commentService.count(admin.getCityId(), params,selectCityId);
			JsonUtils.writeSuccessPageJson("查询成功", logs, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}


	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Comment log = WebUtils.write2bean(request, Comment.class);
			commentService.update(log);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Comment log = commentService.findById(id);
			com.zaofan.mobile.bean.Comment comment = new com.zaofan.mobile.bean.Comment();
			BeanUtils.copyProperties(comment, log);
			if(log.getIsReply() == 1){
				comment.setReply(commentService.getReply(log.getId()));
			}
			JsonUtils.writeSuccessJson("查询成功", log, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}
	
	protected void reply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			String content = request.getParameter("content");
			commentService.reply(id_s,content);
			JsonUtils.writeSuccessJson("回复成功", "",response);
			return;
		}catch (ServiceException e) {
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}
	protected void reply_update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			String content = request.getParameter("content");
			commentService.replyUpdate(id_s,content);
			JsonUtils.writeSuccessJson("修改回复成功", "",response);
			return;
		}catch (ServiceException e) {
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

}
